package com.shoukaiseki.test.spring.springbootdemo;

import com.shoukaiseki.test.spring.springbootdemo.model.Users;
import com.shoukaiseki.test.spring.springbootdemo.service.UsersService;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/")
@SpringBootApplication
@EnableAutoConfiguration
@MapperScan("com.shoukaiseki.test.spring.springbootdemo.dao")
public class MainApplication {

    protected static final Logger logger = LoggerFactory.getLogger(MainApplication.class);

    private static int count=0;

    private static int addCount=0;

    //主类用 Application 注解会报错,需要用 SpringBootApplication 注解
    @Autowired
    private UsersService usersService;

    @RequestMapping("/")
    @ResponseBody
    String home() {
       count++;
        return new StringBuffer("hello.").append(count).toString();
    }


    @RequestMapping("/add")
    @ResponseBody
    String add(@RequestParam(value = "name",required = false) String name) {
        try {
            Users pojo = new Users();
            pojo.setName(name);
            addCount++;
            pojo.setShuliang(addCount);
            usersService.insert(pojo);
        }catch (Exception e){
            logger.error("",e);
            return new StringBuffer("error.").toString();
        }
        return new StringBuffer("success.").toString();
    }

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
